class AttrDict(dict):
    def __init__(self, func=None, *args, **kwargs):
        super(AttrDict, self).__init__(*args, **kwargs)
        self.__dict__ = self
        self.func = func if func else lambda x: [None] * 15

    def __getattr__(self, item):
        return self[item]

    def __setattr__(self, key, value):
        self[key] = value

    def __getitem__(self, item):
        try:
            return super(AttrDict, self).__getitem__(item)
        except KeyError:
            value = self.func(item)
            self.__setitem__(item, value)
            return value



if __name__ == "__main__":
    # usage:传入默认值函数
    func = lambda x: list(range(12))
    dct = AttrDict(func)
    print(dct.kog)
    print(dct)

    # 使用示例
    # dic = AttrDict()
    # dic.year = 2021
    # print(dic['year'])  # 输出：2021
    # print(dic.year)  # 输出：2021

